//2009/08/15 20:11:27
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

class StepperMotor
{
public:
    int rotateToNearest(int m, int cur, vector <int> target)
    {
		long long n = m;
		long long tar[64], current=cur;
		//Take seriously care about the type's length, and expression range.
		//This wastes me 3 hours.
        current = (current%n+n)%n;
        int g = target.size();
        cout << "current: " << current << endl;
        for (int i=0; i<g; i++)
        {
            //cout << target[i];
			tar[i] = target[i];
			tar[i] = (tar[i]%n + n) %n;
            //target[i] = (target[i]%n + n) %n;
            //cout << " : " << target[i] << endl;
        }
        sort(tar, tar+g);
		for(int i=0; i<g; i++) cout << tar[i] << ": ";
		cout << endl;
        int i;
        for (i=0; i<g; i++)
            if (tar[i] >= current)
                break;
        int ret1 = (current - tar[(i-1 + g)%g] + n) % n;
        int ret2 = (tar[i%g] - current + n) % n;
        cout << ret1 <<", " << ret2 << endl;
        return ret2 <= ret1 ? ret2 : -ret1;
    }
};
